package com.reserv.action;

import java.io.IOException;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.reserv.dao.MemberDAO;
import com.reserv.util.TimestampUtil;
import com.reserv.vo.MemberVO;

public class JoinAction implements CommandProcess {

	public ForwardAction requestProcess(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		ForwardAction forward = new ForwardAction();
		
		String agree1 = request.getParameter("agree1");
		String agree2 = request.getParameter("agree2");
		String isJoin = request.getParameter("isJoin");
		
		if(isJoin != null) {
			MemberVO member = new MemberVO();
			member.setUid(request.getParameter("uid"));
			member.setPasswd(request.getParameter("passwd"));
			member.setAnswer(request.getParameter("answer"));
			member.setQuestion(request.getParameter("question"));
			member.setName(request.getParameter("name"));
			
			String birth = request.getParameter("year") + "-" 
					+ request.getParameter("month") + "-" + request.getParameter("day");
			member.setBirth(new TimestampUtil().toTimestamp(birth));
			member.setPhone(
					request.getParameter("phone1") + "-"
							+ request.getParameter("phone2") + "-" + request.getParameter("phone3")
					);
			member.setZipcode(
					request.getParameter("zipcode1") + "-" + request.getParameter("zipcode2")
					);
			member.setAddr1(request.getParameter("addr1"));
			member.setAddr2(request.getParameter("addr2"));
			member.setEmail(request.getParameter("email1") + "@" + request.getParameter("email2"));

			System.out.println(member);
			
			MemberDAO dao = MemberDAO.getInstance();
			dao.createVO(member);
			
			forward.setRedirect(false);
			forward.setPath("/main/index.jsp?body=/join/joinok.jsp");
			return forward;
		} else if((agree1 != null) && (agree2 != null)) {
			Calendar cal = Calendar.getInstance();
			int year = cal.get(Calendar.YEAR);
			request.setAttribute("year", year);
			
			forward.setRedirect(false);
			forward.setPath("/main/index.jsp?body=/join/join.jsp");
		} else {
			forward.setRedirect(false);
			forward.setPath("/main/index.jsp?body=/join/joinAgree.jsp");
		}
		return forward;
	}
}
